package com.ruoyi.yljf.service.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.yljf.domain.Book;
import com.ruoyi.yljf.domain.vo.BookChapterVO;
import com.ruoyi.yljf.service.IBookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.yljf.mapper.BookChapterMapper;
import com.ruoyi.yljf.domain.BookChapter;
import com.ruoyi.yljf.service.IBookChapterService;

import javax.annotation.Resource;

/**
 * 章节内容Service业务层处理
 *
 * @author metaee
 * @date 2025-01-03
 */
@Service
public class BookChapterServiceImpl extends ServiceImpl<BookChapterMapper, BookChapter> implements IBookChapterService {
    @Resource
    private BookChapterMapper bookChapterMapper;
    @Autowired
    private IBookService bookService;

    /**
     * 查询章节内容列表
     *
     * @param bookChapter 章节内容
     * @return 章节内容
     */
    @Override
    public List<BookChapterVO> selectBookChapterList(BookChapter bookChapter) {
        LambdaQueryWrapper<BookChapter> queryWrapper = Wrappers.lambdaQuery();
        if (StringUtils.isNotNull(bookChapter.getBookId())) {
            queryWrapper.eq(BookChapter::getBookId, bookChapter.getBookId());
        }
        if (StringUtils.isNotNull(bookChapter.getNum())) {
            queryWrapper.eq(BookChapter::getNum, bookChapter.getNum());
        }
        if (StringUtils.isNotEmpty(bookChapter.getName())) {
            queryWrapper.like(BookChapter::getName, bookChapter.getName());
        }
        queryWrapper
                .orderByDesc(BookChapter::getBookId)
                .orderByAsc(BookChapter::getNum);
        List<BookChapter> bookChapters = bookChapterMapper.selectList(queryWrapper);
        if (CollectionUtils.isEmpty(bookChapters)) {
            return Collections.emptyList();
        }
        List<Long> bookIds = bookChapters.stream().map(BookChapter::getBookId).collect(Collectors.toList());
        List<Book> books = bookService.listByIds(bookIds);
        List<BookChapterVO> vos = new ArrayList<>();
        for (BookChapter chapter : bookChapters) {
            BookChapterVO bookChapterVO = new BookChapterVO(chapter);
            for (Book book : books) {
                if (chapter.getBookId().equals(book.getId())) {
                    bookChapterVO.setBookName(book.getName());
                    break;
                }
            }
            vos.add(bookChapterVO);
        }
        return vos;
    }

    @Override
    public int saveBookChapter(BookChapter bookChapter) {
        if (isCanAdd(bookChapter.getBookId(), bookChapter.getNum())) {
            return bookChapterMapper.insert(bookChapter);
        }
        return 0;
    }

    @Override
    public int updateBookChapter(BookChapter bookChapter) {
        BookChapter selectedById = bookChapterMapper.selectById(bookChapter.getId());
        // 如果点击修改第n章，不更改章节数可直接修改
        if (Objects.equals(selectedById.getNum(), bookChapter.getNum())) {
            return bookChapterMapper.updateById(bookChapter);
        }
        if (isCanAdd(bookChapter.getBookId(), bookChapter.getNum())) {
            return bookChapterMapper.updateById(bookChapter);
        }
        return 0;
    }

    private boolean isCanAdd(Long bookId, Long chapterNum) {
        // 根据书籍id和章节数查询，如果有则无法插入
        BookChapter isExist = bookChapterMapper.selectOne(Wrappers.lambdaQuery(BookChapter.class).eq(BookChapter::getBookId, bookId).eq(BookChapter::getNum, chapterNum));
        if (isExist != null) {
            throw new ServiceException("该书已存在第:" + chapterNum + "章，请修改章节数");
        }
        return true;
    }
}
